<?php


namespace App\HttpController\Api;


use App\Model\CacheTable;
use App\Model\DataTable;
use App\Model\User;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;
use EasySwoole\Component\Context\ContextManager;

class Task extends Base
{
    public function index()
    {
        $uid = ContextManager::getInstance()->get('user_id');

        $redis = RedisPool::defer();
        $key = CacheTable::INVITE_USER_CACHE . $uid;
        $data = json_decode($redis->get($key), true);
        if (!$data) {
            $db = MysqlPool::defer();
            $res = $db->where('id', $uid)
                ->getOne(DataTable::USER_TABLE, 'invite_count,invite_score,nickname');
            $data['nickname'] = '';
            if ($res) {
                $data['count'] = intval($res['invite_count']);
                $data['score'] = intval($res['invite_score']);
                $data['nickname'] = $res['nickname'];
            } else {
                $data['count'] = 0;
                $data['score'] = 0;
            }
            $redis->set($key, json_encode($data), 7200);
        }
        $data['total_score'] = User::getScore($uid);
        $data['sharelink'] = md5($uid);
        $this->success($data);
        return;
    }
}